<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /* 
            思路：集合范围内无重复元素，所以对candidates进行排序后，虽然每个元素可以调用多次，但是无需去重了
            */
            var combinationSum = function (candidates, target) {
                candidates.sort((a, b) => a - b)
                let res = []
                function backTacking(path, sum, start) {
                    if (sum > target) return
                    if (sum == target) {
                        return res.push([...path])
                    }
                    for (let i = start; i < candidates.length; i++) {
                        path.push(candidates[i])
                        //可以重复选用
                        backTacking(path, sum + candidates[i], i)
                        path.pop()
                    }
                }
                backTacking([], 0, 0)
                return res
            }
        </script>
    </body>
</html>
